package com.scenery.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

import com.scenery.vo.City;
import com.scenery.vo.Scenery;
/***
 * 
 * xml解析类
 * **/
public class XmlPullUtil {
	private static String tag = XmlPullUtil.class.getName();
	public static ArrayList<City> pullCityByAssert(InputStream is) {
		XmlPullParserFactory factory;
		ArrayList<City> cityList = null;
		City city = null;
		try {
			factory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();
			
			String currentTag = null;
			
			//parser.setInput(is, "utf-8");
			parser.setInput(is, "utf-8");
			int type = parser.getEventType();
			while (type != XmlPullParser.END_DOCUMENT) {
				String typeName = parser.getName();
				if (type == XmlPullParser.START_TAG) {
					//Log.v("TAG", "typeName=" + typeName);
					if ("cityList".equals(typeName)) {
						cityList = new ArrayList<City>();
						int count = Integer.parseInt(parser
								.getAttributeValue(0));
						Log.v(tag, count + "");
					}

					else if ("city".equals(typeName)) {
						city = new City();

					} else if ("id".equals(typeName)) {
						currentTag = "id";

					} else if ("name".equals(typeName)) {
						currentTag = "name";
					} else if ("enName".equals(typeName)) {
						currentTag = "enName";
					} else if ("prefixLetter".equals(typeName)) {
						currentTag = "prefixLetter";
					}

				} else if (type == XmlPullParser.END_TAG) {
					if ("city".equals(typeName)) {
						cityList.add(city);
						city = null;
					}

				} else if (type == XmlPullParser.TEXT) {
					String s = parser.getText();
					//Log.v(tag, s);
					if ("rspCode".equals(typeName)) {
						Log.v("TAG", "typeName=" + typeName);
					} else if ("name".equals(currentTag)) {
						city.setName(s);
						currentTag = null;
					} else if ("enName".equals(currentTag)) {
						city.setEnName(s);
						currentTag = null;
					} else if ("prefixLetter".equals(currentTag)) {
						city.setPrefixLetter(s);
						currentTag = null;
					}

				}

				type = parser.next();
			}
			Log.v("TAG", "size=" + cityList.size());

		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cityList;

	}
	public static ArrayList<City> pullCity(String xml) {
		XmlPullParserFactory factory;
		ArrayList<City> cityList = null;
		City city = null;
		try {
			factory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();
			InputStream is = new ByteArrayInputStream(xml.getBytes());
			String currentTag = null;
			
			//parser.setInput(is, "utf-8");
			parser.setInput(is, "utf-8");
			int type = parser.getEventType();
			while (type != XmlPullParser.END_DOCUMENT) {
				String typeName = parser.getName();
				if (type == XmlPullParser.START_TAG) {
					//Log.v("TAG", "typeName=" + typeName);
					if ("cityList".equals(typeName)) {
						cityList = new ArrayList<City>();
						int count = Integer.parseInt(parser
								.getAttributeValue(0));
						Log.v(tag, count + "");
					}

					else if ("city".equals(typeName)) {
						city = new City();

					} else if ("id".equals(typeName)) {
						currentTag = "id";

					} else if ("name".equals(typeName)) {
						currentTag = "name";
					} else if ("enName".equals(typeName)) {
						currentTag = "enName";
					} else if ("prefixLetter".equals(typeName)) {
						currentTag = "prefixLetter";
					}

				} else if (type == XmlPullParser.END_TAG) {
					if ("city".equals(typeName)) {
						cityList.add(city);
						city = null;
					}

				} else if (type == XmlPullParser.TEXT) {
					String s = parser.getText();
					//Log.v(tag, s);
					if ("rspCode".equals(typeName)) {
						Log.v("TAG", "typeName=" + typeName);
					} else if ("name".equals(currentTag)) {
						city.setName(s);
						currentTag = null;
					} else if ("enName".equals(currentTag)) {
						city.setEnName(s);
						currentTag = null;
					} else if ("prefixLetter".equals(currentTag)) {
						city.setPrefixLetter(s);
						currentTag = null;
					}

				}

				type = parser.next();
			}
			Log.v("TAG", "size=" + cityList.size());

		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cityList;

	}

	public static ArrayList<Scenery> pullScenery(String xml) {
		XmlPullParserFactory factory;
		ArrayList<Scenery> list = null;
		Scenery scenery = null;
		try {
			factory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();
			InputStream is = new ByteArrayInputStream(xml.getBytes());
			String currentTag = null;
			parser.setInput(is, "utf-8");
			int type = parser.getEventType();
			while (type != XmlPullParser.END_DOCUMENT) {
				String typeName = parser.getName();
				if (type == XmlPullParser.START_TAG) {
					list=new ArrayList<Scenery>();
					if("sceneryList ".equals(typeName))
					{
						
					}else if ("scenery".equals(typeName)) {
						scenery=new Scenery();
						
					}else if ("sceneryName".equals(typeName)) {
						currentTag = "sceneryName";
						
					}
					else if ("imgPath".equals(typeName)) {
						currentTag = "imgPath";
						
					}else if ("sceneryId".equals(typeName)) {
						currentTag = "sceneryId";
						
					}else if ("sceneryAddress".equals(typeName)) {
						currentTag = "sceneryAddress";
						
					}
				} else if (type == XmlPullParser.END_TAG) {
					if ("scenery".equals(typeName)) {
						list.add(scenery);
						scenery = null;
					}
				}else if (type == XmlPullParser.TEXT) {
					String s = parser.getText();
					Log.v(tag, s);
					if ("sceneryName".equals(currentTag)) {
						scenery.setSceneryName(s);
						currentTag = null;
					} else if ("imgPath".equals(currentTag)) {
						scenery.setImgPath(s);
						currentTag = null;
					} else if ("sceneryId".equals(currentTag)) {
						scenery.setSceneryId(s);
						currentTag = null;
					}else if ("sceneryAddress".equals(currentTag)) {
						scenery.setSceneryAddress(s);
						currentTag = null;
					}
				}
				type = parser.next();
			}
			
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
